<?php
/**
 * 本页面用于单个采集
 * 仅供测试用 
 */
	include_once('../sys/sys.php');
	include_once ('./data/lndt.php');
	header('Content-Type: text/html; charset=utf-8');
	
	$tid = $_GET['tid'];
	
	if(!is_numeric($tid) || $tid < 0){
		show_normal_mesR('TAG ID错误','TAG ID错误','TAG查询');
	}
	//取数据库中的小说信息，用于比较
	$where = 'AND id= ' . $tid .'';
	$dbnov_infos = $DB->build_and_exec_query(
			array('select' => '*','from'   => '7nnovels','where'	=> "1 " . $where ,)
	);
	if(count($dbnov_infos) < 3){
		$dbnov_infos = array();
	}
	//从TID获取总本数以及每本对应连接和更新时间
	$indexurl = make_book_url($ln_base_link,$tid,$ln_n_middle);
	$context = mb_convert_encoding(file_get_http_contents($indexurl),"UTF-8","GBK");
	//获取作者
	$author = explode('作&nbsp;&nbsp;&nbsp; 者：',$context);
	$author = explode('</td>',$author[1]);
	$author = $author[0];
	//获取最后更新时间(用于第一次检测更新)
	$lastupdate = explode('最后更新：',$context);
	$lastupdate = explode('</td>',$lastupdate[1]);
	$lastupdate = $lastupdate[0];
	$lastupdate = strtotime($lastupdate);
	//---------------------------第一次更新比对，如果更新时间相同，则直接结束-----------------
	if ($dbnov_infos['lastupdate'] >= $lastupdate){
		//如果时间相同或数据库时间超前，则不需要更新
		echo "<FONT clolor='red'>本小说已为最新，无需更新</FONT>";
		exit;
	}
	//--------------------------------------------------------------------------------------
	//更新小说信息
	$where = 'AND id= ' . $tid .'';
	$result = $DB->build_and_exec_query(
			array('select' => 'author','from'   => '7nnovels','where'	=> "1 " . $where ,)
	);
	$dbauthor = $result['author'];
	if ($dbauthor != $author){
		//更新作者信息
		$where = 'id= ' . $tid .'';
		$DB->do_update('7nnovels',array('author' => $author,),	$where);
	}
	//--------------------------------------------------------------------------------------
	//获取总本数
	//构造分卷阅读连接
	$stories_link = make_story_url($ln_sbase_link, $tid, $ln_s_middle);
	$context = mb_convert_encoding(file_get_http_contents($stories_link),"UTF-8","GBK");
	$ncount = count(explode('<td colspan="4" class="vcss">', $context)) - 1;
	//获取每本的对应信息
	$nov_links = explode('<td colspan="4" class="vcss">', $context);
	$nov_infos = array();
	unset($nov_links[0]);
	foreach ($nov_links as $k=>$v) {
		$kk = $k-1;
		//获取每本连接
		$vol_link = explode('">分卷阅读', $v);
		$vol_link = explode('href="', $vol_link[0]);
		$nov_infos['vol_link'][$kk] = $vol_link[1];
		//获取每本名称
		$vol_name = explode('[<a href=', $v);
		$nov_infos['vol_name'][$kk] = trim($vol_name[0]);
		//获取每本总章节数
		$li_context = explode('<div id="adbottom">', $v);
		$nov_infos['ncount'][$kk] = count(explode(".html\">", $li_context[0])) - 1;
		//获取每本ID(意义不大)
		$vol_id = explode('">分卷阅读', $v);
		$vol_id = explode('vid=', $vol_id[0]);
		$nov_infos['id'][$kk] = $vol_id[1];
		//插入该本信息
		$where = 'AND id= ' . $nov_infos['id'][$kk] .'';
		//不存在则插入
		if(get_scount($where) < 1){
			do_sinsert($nov_infos['id'][$kk], $tid, addslashes($nov_infos['vol_name'][$kk]),0);
			echo "<font color='green'>第" . $k . "本==" . $nov_infos['vol_name'][$kk] . "==正在创建</font><BR>";
			ob_flush();
		}
		//检查整本小说更新
		//首先检查每个小节是否存在（由于存在不按顺序更新的情况，使用inarray方法进行判定）
		//取出该本对应章节在数据库中的ID数组
		$where = 'AND sid= ' . $nov_infos['id'][$kk] .' AND nid= ' . $tid . '';
		$dbsids = $DB->build_and_exec_query(
				array('select' => 'id','from'   => '7nchapters','where'	=> "1 " . $where ,)
		);
		if ($dbsids == ''){$dbsids = array();};
// 		print_r($dbsids);exit;
		//分离该本当前ID数组
		$li_context[0] = str_replace(array("\r\n","\r","\n"," "), '', $li_context[0]);
		$li_context[0] = str_replace("	", '◎◎◎※※§§§', $li_context[0]);
		$csids = '';
		$match_str = "/◎◎◎※※§§§<ahref=\"(.*).html\">(.*)<\/a>/isU";
		preg_match_all($match_str,$li_context[0],$csids);
		//与数据库中的比对
		foreach ($csids[1] as $ck=>$cv) {
			//如果不在数据库中，插入该小节
			if (!in_array($cv, $dbsids)){
// 				echo "<font color='green'>第" . $k . "本==" . $nov_infos['vol_name'][$kk] . "==正在添加新增小节内容</font><BR>";
// 				ob_flush();
				do_chapter_insert($cv,$tid,$nov_infos['id'][$kk]);
			}
		}
		//小节插入完毕后更新当前这本书的所有信息
		$where = ' id= ' . $nov_infos['id'][$kk] .'';
		$DB->do_update('7nstories',
				array(
						'count'					=> $nov_infos['ncount'][$kk],
				),
				$where
		);
		echo "<font color='green'>第" . $k . "本==" . $nov_infos['vol_name'][$kk] . "==更新完毕</font><BR>";
		ob_flush();
	}
	//更新小说更新时间以及总本数
	$where = 'id= ' . $tid .'';
	$DB->do_update('7nnovels',array('count' => $ncount,'lastupdate' => $lastupdate,),	$where);
	echo "<font color='gray'>全书更新完毕</font><BR>";
	
	
	//-----------------------------------------通用函数定义区
	//章节插入
	function do_chapter_insert($cid,$nid,$sid,$ln_sbase_link = 'http://www.7wenku.com/files/article/' , $ln_c_middle = "html/"){
		$text = '';
		$curl = make_chapter_url($ln_sbase_link,$cid,$nid,$ln_c_middle);
		$ch_context = mb_convert_encoding(file_get_http_contents($curl),"UTF-8","GBK");
		
		$text = explode('<div id="title">', $ch_context);
		//提取该章标题
		$cname = explode('</div>', $text[1]);
		$cname = $cname[0];
		//文本处理部分
		$text = explode('</div>', $text[1]);
		$text = str_replace(array("&nbsp;&nbsp;&nbsp;&nbsp;","<br />","<div id=\"content\">"), '', $text[1]);
		$text = str_replace(array("\r\n\r\n\r\n","\r\n\r\n","\r\r","\n\n"), "\r\n", $text);
		//插入
		//检查是否存在（虽然应该是不存在的）
		$where = 'AND id= ' . $cid .'';
		//不存在则插入
		if(get_ccount($where) < 1){
			do_cinsert($cid, $nid, $sid, $curl, addslashes($cname), addslashes($text));
			echo "<font color='blue'>小节：==" . $cname . "==添加完毕</font><BR>";
			ob_flush();
		}
	}
	//获取当前ID的书是否存在
	function get_scount($where){
		global $DB;
		$result = $DB->build_and_exec_query(
				array('select' => 'count(*) AS count','from'   => '7nstories','where'	=> "1 " . $where ,)
		);
		return $result['count'];
	}
	//获取当前ID章是否存在
	function get_ccount($where){
		global $DB;
		$result = $DB->build_and_exec_query(
				array('select' => 'count(*) AS count','from'   => '7nchapters','where'	=> "1 " . $where ,)
		);
		return $result['count'];
	}
	//插书
	function do_sinsert($id,$nid,$name,$count = 0){
		global $DB;
		$DB->do_insert( '7nstories', array(
				'id'				=> $id,
				'nid'				=> $nid,
				'name'				=> $name,
				'count'				=> $count,
		)		);
		if ($result === false) {
			show_normal_mesR('无法添加当前小说当前本','添加出错','添加');
		}
	}
	//插小节
	function do_cinsert($id,$nid,$sid,$url,$name,$text){
		global $DB;
		$DB->do_insert( '7nchapters', array(
				'id'				=> $id,
				'nid'				=> $nid,
				'url'				=> $url,
				'sid'				=> $sid,
				'name'				=> $name,
				'text'				=> $text,
		)		);
		if ($result === false) {
			show_normal_mesR('无法添加当前小说当前本','添加出错','添加');
		}
	}
	
	
	exit;
	
	
?>